package com.jsoft.service.impl;

import com.jsoft.controller.Code;
import com.jsoft.core.RedisTemplate;
import com.jsoft.entity.Apply;
import com.jsoft.dao.ApplyDao;
import com.jsoft.exception.BusinessException;
import com.jsoft.service.ApplyService;
import com.jsoft.util.AuthUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * (Apply)表服务实现类
 *
 * @author makejava
 * @since 2025-03-14 23:30:25
 */
@Service("applyService")
public class ApplyServiceImpl implements ApplyService {
    @Resource
    private ApplyDao applyDao;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 根据userId查询用户申请过的所有工作
     * @param userId 用户主键
     * @return 查询结果
     */
    @Override
    public List<Apply> getAppliedJobsByUserId(int userId) {
        return applyDao.getAppliedJobsByUserId(userId);
    }

    /**
     * 申请职位
     * @param applyDTO 职位相关信息
     */
    @Override
    public void applyJob(Apply applyDTO) {
        Apply apply = new Apply();
        apply.setUserId(applyDTO.getUserId());
        apply.setJobId(applyDTO.getJobId());
        apply.setUpdateTime(applyDTO.getUpdateTime());
        apply.setStatus(0); // 假设默认状态为0
        applyDao.insert(apply);
    }

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public Apply queryById(Integer id) {
        return this.applyDao.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param apply 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @Override
    public Page<Apply> queryByPage(Apply apply, PageRequest pageRequest) {
        long total = this.applyDao.count(apply);
        return new PageImpl<>(this.applyDao.queryAllByLimit(apply, pageRequest), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param apply 实例对象
     * @return 实例对象
     */
    @Override
    public Apply insert(Apply apply) {
        this.applyDao.insert(apply);
        return apply;
    }

    /**
     * 修改数据
     *
     * @param apply 实例对象
     * @return 实例对象
     */
    @Override
    public Apply update(Apply apply) {
        this.applyDao.update(apply);
        return this.queryById(apply.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.applyDao.deleteById(id) > 0;
    }


    /**
     * 通过用户id和职位id查询申请信息
     * @param userId
     * @param jobId
     * @return
     */
    @Override
    public List<Apply> list(Integer userId, Integer jobId) {
       List<Apply> list = applyDao.list(userId,jobId);
       return list;
    }

    /**
     * 申请工作
     * @param jobId
     */
    @Override
    public void application(Integer jobId) {

        if(!Objects.isNull(AuthUtil.getLoginUser(redisTemplate))&&
                !Objects.isNull(AuthUtil.getLoginUser(redisTemplate))){
            Integer userId = AuthUtil.getLoginUser(redisTemplate).getUser().getId();
            List<Apply> list = list(userId, jobId);
            //若查询到的结果不为空,则职位已经申请
            if(!list.isEmpty()){
                throw new BusinessException("该职位已经申请", Code.BUSSINESS_ERR);
            }
            //为空则申请职位
            Apply apply = new Apply();
            apply.setUserId(userId);
            apply.setJobId(jobId);
            apply.setUpdateTime(new Date());
            apply.setStatus(0);
            applyDao.application(apply);

        }

    }
}
